﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-17 11:28:51
 * 功能：游戏界面的引导
**************************************************************************************/

using Game.BaseData;
using Game.Framework;
using Game.UI.GuidePanel;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.GamePanel
{
    public class Guide
    {
        private const string Start = "StartGuide_GuideKey";
        private const string Bomb = "Bomb_GuideKey";
        private const string Shuffle = "Shuffle_GuideKey";
        private const string AddTime = "AddTime_GuideKey";
        private const string ErrorLimit = "ErrorLimit_GuideKey";
        private const string TimeLimit = "TimeLimit_GuideKey";

        private GamePanelView m_View;
        private List<Item> m_Items;
        private GuideData m_GuideData;
        private Config.Guide m_GuideCfg;

        public Guide(GamePanelView gamePanelView, List<Item> items)
        {
            m_View = gamePanelView;
            m_Items = items;
            m_GuideData = new GuideData();
            m_GuideCfg = BaseDataMgr.Instance.Cfg.intro;
        }

        private bool NoGuideStart(GameDataModel.Level level)
        {
            if (m_GuideCfg.starter.status == 0 || level.level != 1 || PlayerPrefs.HasKey(Start)) return true;

            // 引导第一个格子
            m_GuideData.highArea = m_Items[9].transform as RectTransform;
            m_GuideData.tip = m_GuideCfg.starter.content[0];
            m_GuideData.flipH = false;
            m_GuideData.flipV = false;
            m_GuideData.tipPos = new Vector2(135f, 202f);
            m_GuideData.onClickContinueBtnHandle = () =>
            {
                // 引导第二个格子
                m_GuideData.highArea = m_Items[10].transform as RectTransform;
                m_GuideData.tip = m_GuideCfg.starter.content[1];
                m_GuideData.flipH = true;
                m_GuideData.flipV = true;
                m_GuideData.tipPos = new Vector2(-135f, 260);
                m_GuideData.onClickContinueBtnHandle = () =>
                {
                    // 引导消除所有后有奖励
                    m_GuideData.highArea = m_View.GetRect(GamePanelNotifications.GameArea_RECT);
                    m_GuideData.tip = m_GuideCfg.starter.content[2];
                    m_GuideData.flipH = false;
                    m_GuideData.flipV = false;
                    m_GuideData.tipPos = Vector2.zero;
                    m_GuideData.onClickContinueBtnHandle = () =>
                    {
                        // 引导道具：高亮提示
                        m_GuideData.highArea = m_View.GetControl<Button>(GamePanelNotifications.HintBtn_CTRL).transform as RectTransform;
                        m_GuideData.tip = m_GuideCfg.props.content[0];
                        m_GuideData.flipH = false;
                        m_GuideData.flipV = false;
                        m_GuideData.tipPos = new Vector2(123, -862);
                        m_GuideData.isSmall = false;
                        m_GuideData.isResetHigh = false;
                        m_GuideData.onClickContinueBtnHandle = () => SaveData(Start);
                        EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);
                    };
                    EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);
                };
                EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);
            };

            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        private void SaveData(string key)
        {
            EventCenter.Instance.Trigger(GameEventName.HIDE_GUIDE);
            PlayerPrefs.SetInt(key, 1);
            PlayerPrefs.Save();
        }

        private bool NoBomb()
        {
            if (!GameDataMgr.Instance.IsUnlockBomb() || PlayerPrefs.HasKey(Bomb)) return true;

            // 引导道具：炸弹
            m_GuideData.highArea = m_View.GetControl<Button>(GamePanelNotifications.BombBtn_CTRL).transform as RectTransform;
            m_GuideData.tip = m_GuideCfg.props.content[1];
            m_GuideData.flipH = false;
            m_GuideData.flipV = false;
            m_GuideData.tipPos = new Vector2(195.2f, -820);
            m_GuideData.isSmall = true;
            m_GuideData.isResetHigh = true;
            m_GuideData.onClickContinueBtnHandle = () => SaveData(Bomb);
            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        private bool NoShuffle()
        {
            if (!GameDataMgr.Instance.IsUnlockShuffle() || PlayerPrefs.HasKey(Shuffle)) return true;

            // 引导道具：刷新
            m_GuideData.highArea = m_View.GetControl<Button>(GamePanelNotifications.ShuffleBtn_CTRL).transform as RectTransform;
            m_GuideData.tip = m_GuideCfg.props.content[2];
            m_GuideData.flipH = true;
            m_GuideData.flipV = true;
            m_GuideData.tipPos = new Vector2(27f, -820);
            m_GuideData.isSmall = true;
            m_GuideData.isResetHigh = true;
            m_GuideData.onClickContinueBtnHandle = () => SaveData(Shuffle);
            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        private bool NoAddTime()
        {
            if (!GameDataMgr.Instance.IsUnlockTime() || PlayerPrefs.HasKey(AddTime)) return true;

            // 引导道具：刷新
            m_GuideData.highArea = m_View.GetControl<Button>(GamePanelNotifications.AddTimeBtn_CTRL).transform as RectTransform;
            m_GuideData.tip = m_GuideCfg.props.content[3];
            m_GuideData.flipH = true;
            m_GuideData.flipV = true;
            m_GuideData.tipPos = new Vector2(123f, -820);
            m_GuideData.isSmall = false;
            m_GuideData.isResetHigh = true;
            m_GuideData.onClickContinueBtnHandle = () => SaveData(AddTime);
            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        private bool NoErrorLimit()
        {
            if (!GameDataMgr.Instance.IsUnlockErrorLimit() || PlayerPrefs.HasKey(ErrorLimit)) return true;

            // 引导错误次数限制
            m_GuideData.highArea = m_View.GetControl<Image>(GamePanelNotifications.Star_CTRL).rectTransform;
            m_GuideData.tip = m_GuideCfg.functions.content[0];
            m_GuideData.flipH = true;
            m_GuideData.flipV = false;
            m_GuideData.tipPos = new Vector2(184, 551);
            m_GuideData.isSmall = true;
            m_GuideData.isResetHigh = true;
            m_GuideData.onClickContinueBtnHandle = () => SaveData(ErrorLimit);
            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        private bool NoTimeLimit()
        {
            if (!GameDataMgr.Instance.IsUnlockTimeLimit() || PlayerPrefs.HasKey(TimeLimit)) return true;

            // 引导时间限制
            m_GuideData.highArea = m_View.GetControl<Text>(GamePanelNotifications.Time_CTRL).transform.parent as RectTransform;
            m_GuideData.tip = m_GuideCfg.functions.content[1];
            m_GuideData.flipH = false;
            m_GuideData.flipV = true;
            m_GuideData.tipPos = new Vector2(87, 821);
            m_GuideData.isSmall = false;
            m_GuideData.isResetHigh = true;
            m_GuideData.onClickContinueBtnHandle = () => SaveData(TimeLimit);
            EventCenter.Instance.Trigger(GameEventName.GUIDE, m_GuideData);

            return false;
        }

        public void Cache(GameDataModel.Level level)
        {
            if (!NoGuideStart(level)) return;
            if (!NoBomb()) return;
            if (!NoShuffle()) return;
            if (!NoAddTime()) return;
            if (!NoErrorLimit()) return;
            if (!NoTimeLimit()) return;
        }
    }
}